<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/15
 * Time: 下午2:14
 */

namespace app\src\seller\logic;


use app\src\base\logic\BaseLogic;
use app\src\payment\helper\PayCodeHelper;
use app\src\seller\emnu\SellerOrderStatusEmnu;
use app\src\seller\helper\OrderCodeHelper;
use app\src\seller\model\SellerOrder;
use app\src\insurance\enum\PayEnum;
class SellerOrderLogic extends BaseLogic
{
    protected function _init()
    {
        $this->setModel(new SellerOrder());
    }

    /**
     * 添加订单
     * @param $seller_id
     * @param $order_info
     * @return array
     */
    public function addOrder($seller_id, $order_info)
    {
        $is_first = $this->getInfo(['uid'=>$order_info['uid'],'order_status'=>'1']);
        $first = 0;
        if(empty($is_first['info'])){
            $first = 1;
            $seller = (new SellerLogic())->getInfo(['id'=>$seller_id]);
            $first_offer = $seller['info']['first_offer'];
            $first_offer_num = $seller['info']['first_offer_num'];
            if($first_offer_num > 0&&$seller['info']['lock_first_offer']==0) {
                if ($first_offer == 1000) {
                    if ($order_info['amount'] >= 10000) $order_info['price'] = $order_info['amount'] - $first_offer;
                }
                if ($first_offer == 500) {
                    if ($order_info['amount'] >= 3000){
                        $order_info['price'] = $order_info['amount'] - $first_offer;
                    }

                }
                if ($first_offer == 2000) {
                    if ($order_info['amount'] >= 20000) $order_info['price'] = $order_info['amount'] - $first_offer;
                }
            }
        }

        if($order_info['price']<0) $order_info['price']=1;
        $data = [
            'uid' => $order_info['uid'],
            'amount' => $order_info['amount'],
            'no_discount' => $order_info['no_discount'],
            'price' => $order_info['price'],
            'income' => '0',
            'seller_id' => $seller_id,
            'create_time' => NOW_TIME,
            'pay_time' => 0,
            'order_status' => SellerOrderStatusEmnu::UNPAY,
            'order_code' => '',
            'status' => 0,
            'pay_code' => '',
            'note' => isset($order_info['note'])?$order_info['note']:'',
            'is_first' =>$first,
        ];

        $result = $this->add($data);
        if($result['status']){
            $id = $result['info'];
            $order_code=OrderCodeHelper::create(OrderCodeHelper::SELLER_ORDER, $id);
            $this->saveByID($id, ['order_code' => $order_code]);
            $result['info']=['id'=>$id,'order_code'=>$order_code,'is_first'=>$first,'fee'=>$order_info['price']];
            return $this->apiReturnSuc($result['info']);
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 订单信息
     * @param $order_code
     * @return array
     */
    public function orderInfo($order_code)
    {
        $map = [
            'order_code' => $order_code,
            'status' => 0
        ];

        $result = $this->getInfo($map);

        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if(empty($result['info'])) return $this->apiReturnErr('error order');
        return $this->apiReturnSuc($result['info']);
    }


    /**
     * 取消订单
     * @param $order_code
     * @return array
     */
    public function cancelOrder($order_code)
    {

        $result = $this->orderInfo($order_code);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        $order_status = $result['info']['order_status'];
        if($order_status == SellerOrderStatusEmnu::PAY){
            return $this->apiReturnErr('already pay, can\'t cancel');
        }

        if($order_status == SellerOrderStatusEmnu::CANCEL){
            return $this->apiReturnSuc('success');
        }

        //未支付才能取消
        $map = [
            'order_code' => $order_code,
            'status' => 0
        ];
        $result = $this->save($map, ['order_status' => SellerOrderStatusEmnu::CANCEL]);
        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 订单状态是否正常
     * @param $order_code
     * @return bool
     */
    public function checkOrder($order_code, $order_status = false)
    {
        $map = [
            'order_code' => $order_code,
            'status' => 0
        ];

        if($order_status && in_array($order_status, [
                SellerOrderStatusEmnu::UNPAY,
                SellerOrderStatusEmnu::PAY,
                SellerOrderStatusEmnu::CANCEL
            ])) {
            $map['order_status'] = $order_status;
        }

        $result = $this->getInfo($map);
        if(!$result['status']) return $result['info'];
        if(!empty($result['info'])){
            return true;
        }else{
            return false;
        }
    }




    /*
     * 查询某人的订单
     */
    public function queryConsume($uid,$page){
        $map=['uid'=>$uid];$map['order_status']=1;
        $query = $this->getModel();
        $order = 'create_time desc';
        $list = $this->queryWithPagingHtml($map,$page,$order);
        if(!empty($list['info']['list'])){
            foreach($list['info']['list'] as $k=>$v){
                $tem[$k]=$v;
                $seller = (new SellerLogic())->getInfo(['id'=>$v['seller_id']]);
                $tem[$k]['seller_name'] = $seller['info']['name'];
            }
        }else{
            $tem=[];
        }
        $count =$this->count($map);
        return $this->apiReturnSuc(['count'=>$count['info'],'list'=>$tem]);
    }

}